package com.zhaoye.service.impl;

import com.zhaoye.dao.OrderDao;
import com.zhaoye.domain.Order;
import com.zhaoye.service.AccountService;
import com.zhaoye.service.OrderService;
import com.zhaoye.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author feng
 * @Date 2021/11/21 21:54
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("---->开始创建订单");
        orderDao.create(order);
        //扣减库存
        log.info("--------->开始调用库存，做扣减");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("--------->调用库存，做扣减结束");
        //扣钱
        log.info("-------->订单微服务开始调用账户，做扣减money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("------->订单调用账户，减money结束");

        // 4.修改订单的状态，从0到1
        log.info("---->修改订单状态，开始");
        orderDao.update(order.getUserId(), 0);
        log.info("---->修改订单状态，结束");

        log.info("---->下订单结束了，(*^_^*)");
    }
}
